package com.danco.training.zaneuskaya.controller;


import java.io.Serializable;

import com.danco.training.zaneuskaya.api.BookController;
import com.danco.training.zaneuskaya.api.BookService;
import com.danco.training.zaneuskaya.api.ClaimController;
import com.danco.training.zaneuskaya.api.ClaimService;
import com.danco.training.zaneuskaya.dependency.DependencyConfiguration;
import com.danco.training.zaneuskaya.domain.Claim;
import com.danco.training.zaneuskaya.view.IOUtil;



public class ClaimControllerImpl implements Serializable, ClaimController{
	

	private static final String READ_CLAIM_LIST_FROM_FILE_MESSAGE = "Enter directory and file name  :";

	private static final String SAVED_BY_DEFAULT_IN = "Saved by default in ";

	private static final String FILE_TO_SAVE_DESTINATION = "d:\\file2.csv";

	private static final String SAVE_CLAIM_LIST_TO_FILE_MESSAGE = "Enter directory and file name for saving";

	private static final String SUBMIT_CLAIM_MESSAGE = "Enter claim's to submit Id";

	private static final String DELETE_CLAIM_MESSAGE = "Enter claim's id to remove ";

	private static final String UPDATE_CLAIM_MESSAGE = "Enter claim's Id for modifying";

	private static final String CLAIM_NOT_EXISTED = "Claim not existed";

	private static final String PRINT_CLAIM_INFO_MESSAGE = "Enter claim Id to show";

	private static final String ENTER_AMOUNT_OF_THE_CLAIM = "Enter amount of the claim";

	private static final String ENTER_BOOK_ID_FOR_CLAIM_CREATION = "Enter book id for claim creation ";

	private static final String PRINT_CLAIM_LIST_SORTED_BY_CLAIM_AMOUNT_MESSAGE = "Claims in ClaimStorage sorted by claim amount";

	private static final String PRINT_CLAIM_LIST_SORTED_BY_ALPHABET_MESSAGE = "Claims in ClaimStorage sorted by alphabet";

	private static final String PRINT_CLAIM_LIST_MESSAGE = "Claims in ClaimStorage";

	/**
	 * 
	 */
	private static final long serialVersionUID = 3686885504220073158L;

	private ClaimService claimService = (ClaimService) DependencyConfiguration.getInstanceByClassName(ClaimService.class);
	
	private BookService bookService = (BookService) DependencyConfiguration.getInstanceByClassName(BookService.class);
	
	private BookController bookController = (BookController) DependencyConfiguration.getInstanceByClassName(BookController.class);
	

	public void printClaimList() {
		IOUtil.write(PRINT_CLAIM_LIST_MESSAGE);
		for(Claim claim:claimService.getList() ){
			IOUtil.write(claimInfo(claim));
		}
	}

	public void printClaimListSortedByAlphabet() {
		IOUtil.write(PRINT_CLAIM_LIST_SORTED_BY_ALPHABET_MESSAGE);
		for(Claim claim:claimService.sortClaimsByAlphabet(claimService.getList())){
			IOUtil.write(claimInfo(claim));
			}

	}

	public void printClaimListSortedByClaimAmount() {
		IOUtil.write(PRINT_CLAIM_LIST_SORTED_BY_CLAIM_AMOUNT_MESSAGE);
		for(Claim claim:claimService.sortClaimsByClaimAmount(claimService.getList())){
			IOUtil.write(claimInfo(claim));
			}
		

	}

	public void createClaim(){
		
		IOUtil.write(ENTER_BOOK_ID_FOR_CLAIM_CREATION);
		bookController.printBookList();
		int bookId = IOUtil.readInt();
		IOUtil.write(ENTER_AMOUNT_OF_THE_CLAIM);
		int claim = IOUtil.readInt();
		claimService.addEntity(new Claim(claimService.generateUnexistedId(), bookService.getById(bookId), claim));

	}

	
	public void printClaimInfo() {

		IOUtil.write(PRINT_CLAIM_INFO_MESSAGE);
		int id = IOUtil.readInt();
		while (claimService.getById(id)== null){
			IOUtil.write(CLAIM_NOT_EXISTED);
			IOUtil.write(PRINT_CLAIM_INFO_MESSAGE);
			id = IOUtil.readInt();
		}
		IOUtil.write(claimInfo(claimService.getById(id)));
		

	}

	public void updateClaim() {
		IOUtil.write(UPDATE_CLAIM_MESSAGE);
		int id = IOUtil.readInt(); 
		while (claimService.getById(id)== null){
			IOUtil.write(CLAIM_NOT_EXISTED);
			IOUtil.write(UPDATE_CLAIM_MESSAGE);
			id = IOUtil.readInt();
		}
		Claim claim = claimService.getById(id);
		IOUtil.write(ENTER_AMOUNT_OF_THE_CLAIM);

		if (!IOUtil.read().isEmpty() ){
			claim.setClaimAmount(IOUtil.readInt());
		}
		IOUtil.write(claimInfo(claimService.getById(id)));

	}

	public void deleteClaim(){
		IOUtil.write(DELETE_CLAIM_MESSAGE);
		int id = IOUtil.readInt();
		while (claimService.getById(id)== null){
			IOUtil.write(CLAIM_NOT_EXISTED);
			IOUtil.write(DELETE_CLAIM_MESSAGE);
			id = IOUtil.readInt();
		}
		claimService.removeEntityById(id);
		
	}

	public void submitClaim() {
		IOUtil.write(SUBMIT_CLAIM_MESSAGE);
		int id = IOUtil.readInt();
		while (claimService.getById(id)== null){
			IOUtil.write(CLAIM_NOT_EXISTED);
			IOUtil.write(SUBMIT_CLAIM_MESSAGE);
			id = IOUtil.readInt();
		}
		claimService.addEntity(claimService.getById(id));
		
	}

	public void saveClaimListToFile(){
        IOUtil.write(SAVE_CLAIM_LIST_TO_FILE_MESSAGE);
		String file = IOUtil.read();
		if (file.isEmpty()){
			file = FILE_TO_SAVE_DESTINATION;
			IOUtil.write(SAVED_BY_DEFAULT_IN+ FILE_TO_SAVE_DESTINATION);
			
		}
		claimService.saveEntityListToFile(file);
		
	}
	
	public void readClaimListFromFile(){
		
			IOUtil.write(READ_CLAIM_LIST_FROM_FILE_MESSAGE);
			String file = IOUtil.read();
			if (file.isEmpty()){
				file = FILE_TO_SAVE_DESTINATION;
				IOUtil.write(SAVED_BY_DEFAULT_IN + FILE_TO_SAVE_DESTINATION);
				
				
			}
			   for (Claim c:claimService.readEntityList(file)){
				   claimService.addEntity(c);
				    
			   }
				
			}
	public String claimInfo(Claim c){
		
		StringBuffer str = new StringBuffer();
		str.append(c.getId());
		str.append(" ");		
		str.append(c.getBook().getId());
		str.append(" ");
		str.append(c.getClaimAmount());
		
		return str.toString();
	}

		}


